package com.jqq.generic.homework;

import org.junit.Test;

import java.util.*;

/**
 * @author 姬倩倩
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) {
        DAO<User> userDAO = new DAO<>();
        //增
        userDAO.save("1001", new User(1001, 20, "jack1"));
        userDAO.save("1002", new User(1002, 21, "jack2"));
        userDAO.save("1003", new User(1003, 23, "jack3"));
        //遍历输出
        System.out.println("=====当前的用户=====");
        for ( User user:userDAO.list()) {
            System.out.println(user);
        }
        System.out.println("=====id为1001的用户=====");
        System.out.println(userDAO.get("1001"));
        //改
        userDAO.update("1001", new User(1001, 33, "smith"));
        System.out.println("=====更新后的id为1001的用户=====");
        System.out.println(userDAO.get("1001"));
        //删
        userDAO.delete("1003");
        System.out.println("=====当前的用户=====");
        for ( User user:userDAO.list()) {
            System.out.println(user);
        }

    }
}
class DAO<T>{
    public Map<String, T> map = new HashMap<>();

    public void save(String id, T entity){
        map.put(id, entity);
    }
    public T get(String id){
        return map.get(id);
    }
    public void update(String id, T entity){
        map.put(id, entity);
    }

    public List<T> list(){
        List<T> ts = new ArrayList<>();
        for (Map.Entry<String, T> entry : map.entrySet()) {
            T value = entry.getValue();
            ts.add(value);
        }
        return ts;

    }
    public void delete(String id){
        map.remove(id);
    }
}
class User{
    private int id;
    private int age;
    private String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}